<?php

/**
 * @author HungPV <phamvanhung0818@gmail.com>
 * @todo managment news controller
 */
class news extends MX_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('category');
        $this->load->model('news_model');
        $this->load->library('pagination');
        date_default_timezone_set('Asia/Ho_Chi_Minh');
    }

    function index($row = 0) {
        $this->user->check_permission();
        //paging
        include('paging.php');
        $config['per_page'] = 2;
        $config['base_url'] = base_url() . "/administrator/news/index/";

        $lst_news = $this->news_model->get(-1, $config['per_page'], $row);

        $config['total_rows'] = $this->news_model->get_count();
        $config['cur_page'] = $row;
        $this->pagination->initialize($config);
        $data['list_link'] = $this->pagination->create_links();
        //data tranfer        
        $data['lst_news'] = $lst_news;

        $data['view'] = 'news_index';
        $this->load->view('template', $data);
    }

    function create() {
        $this->user->check_permission();
        if ($this->input->post('title')) {
            $name = $this->input->post('title');
            $description = $this->input->post('description');
            $content = $this->input->post('content');
            $image = $this->input->post('hdffeatured_image');
            $cat_id = $this->input->post('news_cat');
            $permalink = $this->news_model->generate_permalink($name);

            if ($this->news_model->add($name, $description, $content, $image, $cat_id, $permalink)) {
                $this->session->set_flashdata('insert_news_success', true);
            } else {
                $this->session->set_flashdata('insert_news_error', true);
            }
            redirect('administrator/news/create');
        }

        // get list categories
        $categories = $this->category->get();
        $data['categories'] = $categories;

        $data['view'] = 'news_create';
        $this->load->view('template', $data);
    }

    function edit($id) {
        $this->user->check_permission();
        if ($this->input->post('title')) {
            $id = $this->input->post('id');
            $name = $this->input->post('title');
            $description = $this->input->post('description');
            $content = $this->input->post('content');
            $image = $this->input->post('hdffeatured_image');
            $cat_id = $this->input->post('news_cat');            

            if ($this->news_model->edit($id,$name, $description, $content, $image, $cat_id)) {
                $this->session->set_flashdata('update_news_success', true);
            } else {
                $this->session->set_flashdata('update_news_error', true);
            }
            redirect('administrator/news/edit/'.$id);
        }
        
        $news = $this->news_model->get($id);
        $data['news'] = $news;
        // get list categories
        $categories = $this->category->get();
        $data['categories'] = $categories;
        
        $data['view'] = 'news_edit';
        $this->load->view('template', $data);
    }

    function delete() {
        $this->user->check_permission();
        $id = $this->input->post('id');
        if ($this->news_model->delete($id)) {
            echo json_encode(array('result' => TRUE));
        } else {
            echo json_encode(array('result' => FALSE));
        }
    }

}

?>
